package com.guoyh.bilui.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.ColorRes;
import androidx.core.graphics.drawable.DrawableCompat;

import com.xuexiang.xui.utils.ResUtils;

/**
 * @author chaychan
 * @date 2017/3/7  17:19
 */
public class UIUtils {
    /**
     * dip-->px
     */
    public static int dip2Px(Context context, int dip) {
        float density = context.getResources().getDisplayMetrics().density;
        int px = (int) (dip * density + 0.5f);
        return px;
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }


    public static int getColorm(String colors) {
        return getColorm(colors,"FF");
    }

    public static int getColorm(String colors, String alpha) {
        return Color.parseColor(
                colors.replace(
                        "#",
                        "#"+alpha
                )
        );
    }


    public static int getColor(Context context, int colorId) {
        return context.getResources().getColor(colorId);
    }

    public static Drawable getDrawable(Context context, int resId) {
        return context.getResources().getDrawable(resId);
    }

    public static void setImageViewColor(ImageView view, int colorResId) {
        if (view == null) {
            return;
        }
        //mutate()
        Drawable modeDrawable = null;
        try {
            modeDrawable = view.getDrawable().mutate();

        } catch (Exception e) {
        }
        if (modeDrawable == null) {
            return;
        }
        Drawable temp = DrawableCompat.wrap(modeDrawable);
        if (temp == null) {
            return;
        }

        ColorStateList colorStateList = ColorStateList.valueOf(colorResId);
        DrawableCompat.setTintList(temp, colorStateList);
        view.setImageDrawable(temp);
    }

}
